package com.jia.product.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.product.entity.*;
import com.jia.product.entity.Attr;
import com.jia.product.feign.coupon.CouponFeignClient;
import com.jia.commons.sku.SkuReduction;
import com.jia.product.feign.coupon.SpuBounds;
import com.jia.product.mapper.SpuInfoMapper;
import com.jia.product.service.*;
import com.jia.product.vo.spu.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * spu信息(SpuInfo)表服务实现类
 *
 * @author makejava
 * @since 2025-07-08 18:15:29
 */
@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
        implements SpuInfoService {

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    private SpuImagesService spuImagesService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private CouponFeignClient couponFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void compoundAddition(SpuSaveVo spuSaveVo) {
        // 1. 保存spu的基本信息 pms_spu_info
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuSaveVo, spuInfo);
//        spuInfo.setCreateTime(new Date());
//        spuInfo.setUpdateTime(new Date());
        this.saveBaseInfo(spuInfo);
        Long spuInfoId = spuInfo.getId();
        Long brandId = spuInfo.getBrandId();
        Long catalogId = spuInfo.getCatalogId();

        // 2. 保存spu的描述图片 pms_spu_info_desc
        List<String> decript = spuSaveVo.getDecript();
        SpuInfoDesc spuInfoDesc = new SpuInfoDesc();
        spuInfoDesc.setSpuId(spuInfoId);
        spuInfoDesc.setDecript(String.join(",", decript));
        spuInfoDescService.saveBaseInfo(spuInfoDesc);

        // 3. 保存spu的图片集 pms_spu_images
        List<String> images = spuSaveVo.getImages();
        spuImagesService.saveBaseImages(spuInfoId, images);

        // 4. 保存spu的规格参数 pms_product_attr_value
        List<BaseAttrs> baseAttrs = spuSaveVo.getBaseAttrs();
        List<ProductAttrValue> collect = baseAttrs.stream().map(item -> {
            Long attrId = item.getAttrId();
            ProductAttrValue productAttrValue = new ProductAttrValue();
            productAttrValue.setAttrId(attrId);
            productAttrValue.setSpuId(spuInfoId);
            productAttrValue.setAttrValue(item.getAttrValues());
            productAttrValue.setQuickShow(item.getShowDesc());
            Attr attr = attrService.getById(attrId);
            if (attr != null) {
                productAttrValue.setAttrName(attr.getAttrName());
            } else {
                productAttrValue.setAttrName("");
            }
            return productAttrValue;
        }).collect(Collectors.toList());
        productAttrValueService.saveBatch(collect);

        // 5. 保存当前spu对应的所有sku信息 pms_sku_info pms_sku_images pms_sku_sale_attr_value
        List<Skus> skus = spuSaveVo.getSkus();
        if (skus != null && !skus.isEmpty()) {
            skus.forEach(item -> {
                String defaultImg = "";
                // 寻找默认照片
                for (Images image : item.getImages()) {
                    if (image.getDefaultImg() == 1) {
                        defaultImg = image.getImgUrl();
                    }
                }
                SkuInfo skuInfo = new SkuInfo();
                BeanUtils.copyProperties(item, skuInfo);
                skuInfo.setSpuId(spuInfoId);
                skuInfo.setBrandId(brandId);
                skuInfo.setCatalogId(catalogId);
                skuInfo.setSaleCount(0L);
                skuInfo.setSkuDefaultImg(defaultImg);
                skuInfoService.saveBaseSkuInfo(skuInfo);

                Long skuId = skuInfo.getSkuId();

                // 图片
                List<SkuImages> imagesCollect = item.getImages().stream().map(img -> {
                    SkuImages skuImages = new SkuImages();
                    skuImages.setSkuId(skuId);
                    skuImages.setImgUrl(img.getImgUrl());
                    skuImages.setDefaultImg(img.getDefaultImg());
                    return skuImages;
                }).collect(Collectors.toList());
                skuImagesService.saveBatch(imagesCollect);

                // 销售属性
                List<com.jia.product.vo.spu.Attr> attrs = item.getAttrs();
                List<SkuSaleAttrValue> skuSaleCollect = attrs.stream().map(attr -> {
                    SkuSaleAttrValue skuSaleAttrValue = new SkuSaleAttrValue();
                    BeanUtils.copyProperties(attr, skuSaleAttrValue);
                    skuSaleAttrValue.setSkuId(skuId);
                    return skuSaleAttrValue;
                }).collect(Collectors.toList());
                skuSaleAttrValueService.saveBatch(skuSaleCollect);

                // 7. 优惠 满减 信息 跨库
                SkuReduction skuReduction = new SkuReduction();
                BeanUtils.copyProperties(item, skuReduction);
                skuReduction.setSkuId(skuId);
                couponFeignClient.saveSkuReduction(skuReduction);

            });


        }

        // 6. spu的积分信息 跨库
        Bounds bounds = spuSaveVo.getBounds();
        SpuBounds spuBounds = new SpuBounds();
        BeanUtils.copyProperties(bounds, spuBounds);
        spuBounds.setSpuId(spuInfoId);
        spuBounds.setWork(1);
        couponFeignClient.saveSpuBounds(spuBounds);

    }

    @Override
    public void saveBaseInfo(SpuInfo spuInfo) {
        this.baseMapper.insert(spuInfo);
    }




}

